﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VCDIFF.Serialization
{
    public class Deserialize
    {
        protected string Path { get; set; }

        /// <summary>
        /// Deserialize constructor.
        /// </summary>
        /// <param name="path">File path.</param>
        public Deserialize(string path)
        {
            Path = path;
        }

        /// <summary>
        /// Reads a file.
        /// </summary>
        /// <returns>The byte table of the reading file.</returns>
        public byte[] Read()
        {
            FileStream fs = File.OpenRead(Path);
            BinaryReader br = new BinaryReader(fs);
            try
            {                
                return br.ReadBytes((int)fs.Length);
                /*
                int size, offset = 0;

                while (offset < (int)fs.Length)
                {
                    if (fs.Length - offset < 1024)
                        size = (int)fs.Length - offset;
                    else
                        size = 1024;

                    byte[] sequence = new byte[size];
                    sequence = br.ReadBytes(size);

                    // Processing with sequence                    

                    offset += sequence.Length;                                   
                }
                */
            }
            finally
            {
                br.Close();
                fs.Close();
            }
        }

        /// <summary>
        /// Reads a file and returns the chars table of file.
        /// </summary>
        /// <returns>Chars table of file.</returns>
        public char[] ReadChars()
        {
            FileStream fs = File.OpenRead(Path);
            BinaryReader br = new BinaryReader(fs);
            try
            {
                return br.ReadChars((int)fs.Length);
            }
            finally
            {
                br.Close();
                fs.Close();
            }
        }

        /// <summary>
        /// Reads a file and retuns the file string.
        /// </summary>
        /// <returns>File string.</returns>
        public string ReadString()
        {
            StringBuilder data = new StringBuilder();

            foreach (char c in ReadChars())
                data.Append(c);

            return data.ToString();
        }
    }
}
